<template>
  <div class="prj-edit-plan-view">

    <div class="form-view">
      <div style="display: flex;align-items: center">
        <el-form :model="datas" :inline="true" class="demo-form-inline" label-position="left">
          <el-form-item label="项目研发模板">

            <el-select v-model="datas.project_template_name" placeholder="请选择模板" @change="templateChange">
              <el-option
                v-for="item in modelList"
                :key="item.project_template_name"
                :label="item.project_template_name"
                :value="item.project_template_name">
              </el-option>
            </el-select>

          </el-form-item>
          <el-form-item label="项目计划开始时间">
            <el-date-picker
              v-model="datas.plan_start_time"
              type="date"
              @change="startChange"
              placeholder="选择日期"
              :picker-options="pickerOptions">
            </el-date-picker>
            <el-popover
              v-if="isOutStart.length"
              placement="top-start"
              width="100"
              trigger="hover">
              <div>
                <div v-if="isOutStart.length">存在任务开始时间早于项目开始时间</div>
              </div>
              <i slot="reference" class="el-icon-warning-outline" style="color: red;margin-left: 10px"></i>
            </el-popover>

          </el-form-item>
          <el-form-item label="项目计划结束时间">
            <el-date-picker
              v-model="datas.plan_end_time"
              type="date"
              @change="endChange"
              placeholder="选择日期"
              :picker-options="pickerOptions2">
            </el-date-picker>
            <el-popover
              v-if="isOutEnd.length"
              placement="top-start"
              width="100"
              trigger="hover">
              <div>
                <div v-if="isOutEnd.length">存在任务结束时间晚于项目结束时间</div>
              </div>
              <i slot="reference" class="el-icon-warning-outline" style="color: red;margin-left: 10px"></i>
            </el-popover>
          </el-form-item>
          <el-form-item label="项目工期（天）">
            <el-input v-model="datas.duration" placeholder="请输入项目工期" @keyup.native="durationProving" @blur="blur"></el-input>
          </el-form-item>

        </el-form>

        <el-button type="primary" size="mini" @click="refreshDate">重置任务时间</el-button>
      </div>



    </div>


    <div class="top">

      <div class="left">
        <el-form :inline="true">
          <el-form-item label="只看里程碑">
            <el-checkbox v-model="planChecked" @change="planChange"></el-checkbox>
          </el-form-item>
          <el-form-item label="甘特图">
            <el-checkbox v-model="ganttChecked" @change="ganttChange"></el-checkbox>
          </el-form-item>
          <!--          <el-form-item label="关键路径">-->
          <!--            <el-checkbox v-model="keyChecked" @change="keyChange"></el-checkbox>-->
          <!--          </el-form-item>-->
        </el-form>

      </div>

      <div class="right">
        <el-button type="primary" size="mini" @click="onKey">关键路径</el-button>
        <el-button type="primary" size="mini" @click="addPlan">新增里程碑</el-button>
      </div>

    </div>

    <wlGantt
      v-if="ganttChecked"
      ref="wl-gantt-demo"
      :edit="false"
      :is-edit="true"
      :check-source="false"
      :autoGanttDateType="false"
      :useCheckColumn="false"
      :use-check-column="false"
      use-index-column
      :end-date="endDate"
      :start-date="startDate"
      date-type="monthAndDay"
      :data="planChecked?tableData2:tableData"
      :contextMenuOptions="contextMenuOptions"
      :expandRowKeys="expandRowKeys"
      @selection-change="selectionChange"
      @expand-change="expandChange"
      @timeChange="timeChange"
      @taskRemove="taskRemove"
      @preChange="preChange"
      @taskAdd="taskAdd"
    >

      <template>

        <el-table-column
          align="center"
          fixed
          prop="duration"
          width="100"
          label="工期（天）"
        >
        </el-table-column>

        <el-table-column
          align="center"
          fixed
          prop="foreground_task_name"
          width="100"
          label="前置任务"
        >
        </el-table-column>

        <el-table-column
          align="center"
          fixed
          width="100"
          label="负责人"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.person_in_charge && scope.row.person_in_charge.member_id">
              {{scope.row.person_in_charge.member_name}}
            </div>
          </template>
        </el-table-column>


        <el-table-column
          align="center"
          fixed
          type="index"
          width="60"
          label="操作"
        >
          <template slot-scope="scope">

            <el-dropdown>
                <span class="el-dropdown-link">
                  ...
                </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-if="scope.row.level != 5">
                  <a class="opt-btn" style="color: dodgerblue" @click="addChilen(scope.row)">添加子任务</a>
                </el-dropdown-item>
                <el-dropdown-item>
                  <a class="opt-btn" style="color: dodgerblue" @click="editClick(scope.row,scope.$index)">编辑</a>
                </el-dropdown-item>
                <el-dropdown-item v-if="scope.row.is_add">
                  <a class="opt-btn" style="color: dodgerblue" @click="deleteClick(scope.row.id)">删除</a>
                </el-dropdown-item>
                <el-dropdown-item>
                  <a class="opt-btn" style="color: dodgerblue" @click="addFiles(scope.row)">添加文档</a>
                </el-dropdown-item>
                <el-dropdown-item v-if="checkTop(scope.row.id)">
                  <a class="opt-btn" style="color: dodgerblue" @click="topClick(scope.row.id)">上移</a>
                </el-dropdown-item>
                <el-dropdown-item v-if="checkBottom(scope.row.id)">
                  <a class="opt-btn" style="color: dodgerblue" @click="bottomClick(scope.row.id)">下移</a>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>

          </template>
        </el-table-column>
      </template>

    </wlGantt>

    <el-table
      v-else
      size="medium"
      :data="planChecked?tableData2:tableData"
      border
      align="center"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      style="width: 100%">
      <!--        <el-table-column-->
      <!--          prop="no"-->
      <!--          label="序号"-->
      <!--          width="80">-->
      <!--        </el-table-column>-->
      <el-table-column
        align="center"
        type="index"
        width="50px"
        label="序号">
      </el-table-column>
      <el-table-column
        class="name-column"
        min-width="200"
        header-align="center"
        label="里程碑/任务名称">
        <template slot-scope="scope">

          <div style="display: flex;justify-content: space-between;width: 100%">
            <div>
              <span v-if="(!scope.row.children || !scope.row.children.length) && scope.row.level == 1"
                    style="padding-left: 25px">  </span>
              <span>{{scope.row.task_name}}</span>
            </div>
            <el-popover
              v-if="!scope.row.is_add"
              placement="top-start"
              width="100"
              trigger="hover">
              <div style="color: unset">
                模板任务，不可删除
              </div>
              <i slot="reference" class="el-icon-s-opportunity" style="color: unset;margin-left: 10px"></i>
            </el-popover>
          </div>


        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="startDate"
        label="开始日期">
        <template slot-scope="scope">

          <div style="display: flex;align-items: center;justify-content: center;width:100%">
            <span>{{scope.row.startDate ? dateFormat1('yyyy-mm-dd',scope.row.startDate) : ""}}</span>

            <el-popover
              v-if="scope.row.outFront"
              placement="top-start"
              width="100"
              trigger="hover">
              <div style="color: red">
                起始日期超出前置任务结束日期
              </div>
              <i slot="reference" class="el-icon-warning-outline" style="color: red;margin-left: 10px"></i>
            </el-popover>
          </div>

        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="endDate"
        label="结束日期">
        <template slot-scope="scope">
          <div style="display: flex;align-items: center;justify-content: center;width:100%">
            <span>{{scope.row.endDate ? dateFormat1('yyyy-mm-dd',scope.row.endDate) : ""}}</span>
            <el-popover
              v-if="scope.row.is_out"
              placement="top-start"
              width="100"
              trigger="hover">
              <div style="color: red">
                结束日期超出范围
              </div>
              <i slot="reference" class="el-icon-warning-outline" style="color: red;margin-left: 10px"></i>
            </el-popover>
          </div>

        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="foreground_task_name"
        label="前置任务">
      </el-table-column>
      <el-table-column
        align="center"
        prop="duration"
        label="工期（天）">
      </el-table-column>
      <el-table-column
        align="center"
        prop="person"
        label="负责人">
        <template slot-scope="scope">
          <div v-if="scope.row.person_in_charge && scope.row.person_in_charge.member_id">
            {{scope.row.person_in_charge.member_name}}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="days"
        label="参与人">
        <template slot-scope="scope">
          <div v-if="scope.row.task_member && scope.row.task_member.length > 0">
            <el-popover
              placement="top-start"
              width="100"
              trigger="hover">
              <div>
                <div v-for="item in scope.row.task_member" style="padding-bottom: 5px">
                  <i class="el-icon-user">{{ item.member_name}}</i>
                </div>
              </div>
              <i slot="reference" class="el-icon-user">{{scope.row.task_member.length}}</i>
            </el-popover>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="交付文档">
        <template slot-scope="scope">

          <div v-if="scope.row.files.length > 0">
            <el-popover
              placement="top-start"
              width="200"
              trigger="hover">
              <div>
                <div v-for="item in scope.row.files" style="padding-bottom: 5px">
                  <i class="el-icon-document">{{ item.file_name}}</i>
                </div>
              </div>
              <i slot="reference" class="el-icon-document">{{scope.row.files.length}}</i>
            </el-popover>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="阶段性评审">
        <template slot-scope="scope">
          <span>{{scope.row.periodical_review?'是':'否'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="强制验收">
        <template slot-scope="scope">
          <span>{{scope.row.mandatory_check?'是':'否'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作">

        <template slot-scope="scope">

          <el-dropdown>
                <span class="el-dropdown-link">
                  ...
                </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="scope.row.level != 5">
                <a class="opt-btn" style="color: dodgerblue" @click="addChilen(scope.row)">添加子任务</a>
              </el-dropdown-item>
              <el-dropdown-item>
                <a class="opt-btn" style="color: dodgerblue" @click="editClick(scope.row,scope.$index)">编辑</a>
              </el-dropdown-item>
              <el-dropdown-item v-if="scope.row.is_add">
                <a class="opt-btn" style="color: dodgerblue" @click="deleteClick(scope.row.id)">删除</a>
              </el-dropdown-item>
              <el-dropdown-item>
                <a class="opt-btn" style="color: dodgerblue" @click="addFiles(scope.row)">添加文档</a>
              </el-dropdown-item>
              <el-dropdown-item v-if="checkTop(scope.row.id)">
                <a class="opt-btn" style="color: dodgerblue" @click="topClick(scope.row.id)">上移</a>
              </el-dropdown-item>
              <el-dropdown-item v-if="checkBottom(scope.row.id)">
                <a class="opt-btn" style="color: dodgerblue" @click="bottomClick(scope.row.id)">下移</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog
      :title="editTitle"
      v-if="editShow"
      :visible.sync="editShow"
      width="750px"
      :before-close="handleClose">

      <edit-plan-view ref="editPlan" :data="editItem" :documents="documents" :plans="plans"
                      :members="members"></edit-plan-view>

      <span slot="footer" class="dialog-footer">
          <el-button @click="editShow = false">取 消</el-button>
          <el-button type="primary" @click="dialogConfirm">确 定</el-button>
        </span>
    </el-dialog>

    <el-dialog title="关键路径" width="1400px" :visible.sync="keyDialog">
      <wlGantt
        :edit="false"
        :check-source="false"
        :autoGanttDateType="false"
        :useCheckColumn="false"
        :use-check-column="false"
        use-index-column
        :end-date="endDate"
        :start-date="startDate"
        date-type="monthAndDay"
        :data="keyData"
        :contextMenuOptions="contextMenuOptions"
        :expandRowKeys="expandRowKeys"
      >

        <template>

          <el-table-column
            align="center"
            fixed
            prop="duration"
            width="100"
            label="工期（天）"
          >
          </el-table-column>

          <el-table-column
            align="center"
            fixed
            width="100"
            label="负责人"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.person_in_charge && scope.row.person_in_charge.member_id">
                {{scope.row.person_in_charge.member_name}}
              </div>

            </template>
          </el-table-column>

          <el-table-column
            align="center"
            fixed
            label="参与人"
            width="100">
            <template slot-scope="scope">
              <div v-if="scope.row.task_member && scope.row.task_member.length > 0">
                <el-popover
                  placement="top-start"
                  width="100"
                  trigger="hover">
                  <div>
                    <div v-for="item in scope.row.task_member" style="padding-bottom: 5px">
                      <i class="el-icon-user">{{ item.member_name}}</i>
                    </div>
                  </div>
                  <i slot="reference" class="el-icon-user">{{scope.row.task_member.length}}</i>
                </el-popover>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            fixed
            type="index"
            width="60"
            label="操作"
          >
            <template slot-scope="scope">

              <el-dropdown>
                <span class="el-dropdown-link">
                  ...
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>
                    <a class="opt-btn" style="color: dodgerblue" @click="editClick(scope.row,scope.$index)">编辑</a>
                  </el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.is_add">
                    <a class="opt-btn" style="color: dodgerblue" @click="deleteClick(scope.row.id)">删除</a>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </template>

      </wlGantt>
    </el-dialog>

    <document-edit ref="documentEdit" :datas="documentData" :plans="plans" @succeed="documentSucceed"></document-edit>

  </div>
</template>

<script>
    import documentEdit from "./documentEdit";
    import editPlanView from "./editPlanView";
    // import Vue from 'vue'
    // import wlGantt from 'wl-gantt'
    //
    // import "wl-gantt/lib/wl-gantt.css"
    //
    // Vue.use(wlGantt)

    export default {
        name: "planView",
        components: {editPlanView, documentEdit},
        props: {
            documents: {
                type: Object,
                default: function () {
                    return {
                        list: [],
                        id: 0,
                    }
                }
            },
        },
        data: function () {
            return {
                datas:{},
                isOutStart: [],
                isOutEnd: [],
                startDate: '',
                endDate: '',
                data: [], // 数据
                selected: [], // 选中数据
                pickerOptions: {
                    disabledDate: time => {
                        if (time.toLocaleDateString() == new Date().toLocaleDateString()){
                            return false
                        }
                        return time.getTime() < new Date().getTime();
                    }
                },
                pickerOptions2: {
                    disabledDate: time => {
                        let data = new Date();
                        if (this.datas.plan_start_time) {
                            data = new Date(this.datas.plan_start_time.replace(/-/g,'/'))
                        }
                        if (time.toLocaleDateString() == data.toLocaleDateString()){
                            return false
                        }
                        return time.getTime() < data.getTime();
                    }
                },
                contextMenuOptions: [
                    {label: "任务名称", prop: "name"},
                    {label: "计划开始时间", prop: "startDate"},
                    {label: "计划结束时间", prop: "endDate"},
                ],
                expandRowKeys: ["1"],


                ganttChecked: false,
                planChecked: false,
                keyChecked: false,

                tableData: [],
                tableData2: [],
                id: 1,

                editItem: {},   // 创建数据
                isEdit: false,   //是否编辑
                addId: -1,
                editShow: false,   //编辑界面展示
                editTitle:'新增任务',

                modelList: [],

                options: [],
                documentData: {},

                members: [],   //编辑可选择人物

                checkSource: true,

                plans: [],
                noDateList: [],

                keyDialog:false,
                keyData:[],
            }
        },
        mounted: function () {

            this.options = this.$store.getters.sysDic('file_type')
        },
        methods: {
            screenTableData:function(){
                this.tableData2 = [];
                for (let i = 0; i < this.tableData.length; i++) {
                    let data = {...this.tableData[i]};
                    data.children = [];
                    this.tableData2.push(data)
                }
            },
            changeMember: function (member) {
                this.dealMember(this.tableData, member, 1)
            },
            deleteMember: function (member) {
                this.dealMember(this.tableData, member, 2)
            },
            dealMember: function (list, member, state) {
                for (let i = 0; i < list.length; i++) {
                    let data = list[i];
                    if (data.person_in_charge && data.person_in_charge.member_id == member.member_id) {
                        if (state == 1) {
                            data.person_in_charge = {...member};
                        } else {
                            data.person_in_charge = {};
                            data.person_id = ""
                        }

                    }
                    if (data.task_member) {
                        let index = data.task_member.findIndex(a => a.member_id == member.member_id)
                        if (index != -1) {
                            if (state == 1) {
                                this.$set(data.task_member, index, {...member})
                            } else {
                                data.task_member.splice(index, 1)
                            }

                        }
                    }
                    if (data.task_member_id) {
                        let index = data.task_member_id.findIndex(a => a == member.member_id)
                        if (index != -1) {
                            if (state != 1) {
                                data.task_member_id.splice(index, 1)
                            }

                        }
                    }
                    if (data.children && data.children.length > 0) {
                        this.dealMember(data.children, member, state)
                    }
                }
            },
            setData: function (newval) {

                if (newval) {
                    this.datas = newval

                    this.getModelList();

                    if (newval.plan_start_time != '') {
                        this.startDate = this.dealDateStr(new Date(newval.plan_start_time.replace(/-/g,'/')))
                    }
                    if (newval.plan_end_time != '') {
                        this.endDate = this.dealDateStr(new Date(newval.plan_end_time.replace(/-/g,'/')))
                    }
                    if (!newval.project_task){
                        newval.project_task = []
                    }
                    this.tableData = newval.project_task;

                    this.plans = []
                    for (let i = 0; i < this.tableData.length; i++) {
                        this.dealInitData(this.tableData[i]);

                    }

                    this.checkEndData(this.tableData)
                }
            },
            initOldData: function (list) {
                let arr = []
                for (let i = 0; i < list.length; i++) {
                    let data = {...list[i]}
                    if (list[i].children && list[i].children.length > 0) {
                        let children = this.initOldData(list[i].children)
                        data.children = children;
                    }
                    arr.push(data)
                }
                return arr;
            },
            dealInitData: function (data) {
                this.plans.push(data);
                if (Number(data.id) >= this.id) {
                    this.id = Number(data.id) + 1;
                }
                if (data.person_in_charge) {
                    data.person = data.person_in_charge.member_name
                    data.person_id = data.person_in_charge.member_id
                }

                data.name = data.task_name;
                data.startDate = data.start_date;
                data.endDate = data.end_date;

                if (this.datas.plan_start_time && data.start_date) {
                    if (new Date(this.datas.plan_start_time.replace(/-/g,'/')).getTime() > new Date(data.start_date.replace(/-/g,'/')).getTime()) {
                        this.isOutStart.push(data.id);
                    }
                }
                if (this.datas.plan_end_time && data.end_date) {
                    if (new Date(this.datas.plan_end_time.replace(/-/g,'/')).getTime() < new Date(data.end_date.replace(/-/g,'/')).getTime()) {
                        this.isOutEnd.push(data.id);
                    }
                }

                if (data.files && data.files.length){
                    data.files.forEach(file=>{
                        if (file.id >= this.documents.id){
                            this.documents.id = Number(file.id) + 1
                        }
                    })
                }
                // data.task_member_id = [];
                // if (data.task_member){
                //     data.task_member.forEach(a=>{
                //         data.task_member_id.push(a.member_id)
                //     })
                // }
                if (data.children) {
                    for (let i = 0; i < data.children.length; i++) {
                        this.dealInitData(data.children[i]);
                    }
                }else {
                    data.children = []
                }
            },
            addFiles: function (item) {

                this.documentData = {
                    id: "0",
                    file_name: '',
                    file_type: '',
                    type_name: '',
                    need: false,
                    task_id: item.id + "",
                    task_name: item.task_name,
                    is_add: true,
                }

                if (this.$refs.documentEdit) {
                    this.$refs.documentEdit.openDialog();
                }
            },
            documentSucceed: function () {
                let data = {...this.documentData}

                data.id = this.documents.id + ""
                this.documents.id++;
                this.documents.list.push(data)
                this.addDoc(data)
            },
            checkOutDate: function (list) {

                for (let i = 0; i < list.length; i++) {
                    let data = list[i];

                    if (this.datas.plan_start_time && data.start_date) {
                        if (new Date(this.datas.plan_start_time.replace(/-/g,'/')).getTime() > new Date(data.start_date.replace(/-/g,'/')).getTime()) {
                            this.isOutStart.push(data.id);
                        } else {
                            let index = this.isOutStart.findIndex(a => a == data.id)
                            if (index != -1) {
                                this.isOutStart.splice(index, 1)
                            }
                        }
                    } else {
                        let index = this.isOutStart.findIndex(a => a == data.id)
                        if (index != -1) {
                            this.isOutStart.splice(index, 1)
                        }
                    }
                    if (this.datas.plan_end_time && data.end_date) {
                        if (new Date(this.datas.plan_end_time.replace(/-/g,'/')).getTime() < new Date(data.end_date.replace(/-/g,'/')).getTime()) {
                            this.isOutEnd.push(data.id);
                        } else {
                            let index = this.isOutEnd.findIndex(a => a == data.id)
                            if (index != -1) {
                                this.isOutEnd.splice(index, 1)
                            }
                        }
                    } else {
                        let index = this.isOutEnd.findIndex(a => a == data.id)
                        if (index != -1) {
                            this.isOutEnd.splice(index, 1)
                        }
                    }
                    if (data.children && data.children.length) {
                        this.checkOutDate(data.children);
                    }
                }
            },
            startChange: function (time) {
                if (this.datas.plan_start_time) {
                    this.datas.plan_start_time = this.dealDateStr(this.datas.plan_start_time) + " 00:00:00"
                    this.startDate = this.dealDateStr(new Date(this.datas.plan_start_time.replace(/-/g,'/')))
                    this.checkOutDate(this.tableData)
                } else {
                    this.datas.duration = 0
                    this.isOutStart = []
                }

                if (this.datas.plan_start_time) {
                    if (this.datas.duration) {
                        this.datas.plan_end_time = this.dealDate1(this.datas.plan_start_time, this.datas.duration) + " 23:59:59"
                    } else if (this.datas.plan_end_time) {
                        let data1 = new Date(this.datas.plan_start_time.replace(/-/g,'/'))
                        let data2 = new Date(this.datas.plan_end_time.replace(/-/g,'/'))
                        if (data2.getTime() > data1.getTime()) {
                            let num = ((data2 - data1) / (1 * 24 * 60 * 60 * 1000)).toFixed(0)
                            this.datas.duration = Number(num);
                        }
                    }
                }

                // if (this.datas.plan_end_time && this.datas.plan_start_time){
                //     let data1 = new Date(this.datas.plan_start_time)
                //     let data2 = new Date(this.datas.plan_end_time)
                //     if (data1.getTime() > data2.getTime()){
                //         if (this.datas.duration){
                //             this.datas.plan_end_time = this.dealDate1(this.datas.plan_start_time,this.datas.duration) + " 23:59:59"
                //         }else {
                //             this.datas.plan_end_time = ""
                //         }
                //     }
                // }
            },
            endChange: function (time) {
                if (this.datas.plan_end_time) {
                    this.datas.plan_end_time = this.dealDateStr(this.datas.plan_end_time) + " 23:59:59"
                    this.endDate = this.dealDateStr(new Date(this.datas.plan_end_time.replace(/-/g,'/')))
                    if (this.datas.plan_start_time) {
                        let startDate = Date.parse(this.datas.plan_start_time.replace(/-/g,'/'));
                        let endDate = Date.parse(this.datas.plan_end_time.replace(/-/g,'/'));
                        if (startDate > endDate) {
                            this.datas.duration = 0;
                        }
                        if (startDate == endDate) {
                            this.datas.duration = 0
                        }
                        let num = ((endDate - startDate) / (1 * 24 * 60 * 60 * 1000)).toFixed(0)
                        this.datas.duration = Number(num);
                    }
                    this.checkOutDate(this.tableData)
                } else {
                    this.datas.duration = 0
                    this.isOutEnd = []
                }
            },
            blur: function () {
                if (this.datas.plan_start_time && Number(this.datas.duration)) {

                    this.datas.plan_end_time = this.dealDate1(this.datas.plan_start_time, this.datas.duration) + " 23:59:59"

                    this.checkOutDate(this.tableData)
                } else {

                }
                // else if (this.data.endDate != ""){
                //     this.data.startDate = this.dealDate2(this.data.endDate,this.data.days)
                // }
            },
            durationProving: function (val) {
                let str = this.datas.duration + ""

                // this.form.skus[e].Price 是input的值　　
                // 先把非数字的都替换掉，除了数字和.
                str = str.replace(/[^\d]/g, '');

                this.datas.duration = Number(str);

            },
            dealDate1: function (str, days) {
                let date = new Date(str.replace(/-/g,'/'));

                if (days > 0) {
                    days = days - 1
                }
                let timeStr = Number(days) * 24 * 3600;
                if (date.getTime() > 99999999999) {
                    timeStr = timeStr * 1000;
                }

                let dd = date.getTime() + timeStr

                let date2 = new Date(dd)


                return this.dealDateStr(date2)
            },
            dealDateStr: function (date) {
                let year = date.getFullYear();
                let month = date.getMonth() + 1;
                let day = date.getDate();
                if (month < 10) {
                    month = "0" + month
                }
                if (day < 10) {
                    day = "0" + day;
                }

                return year + "-" + month + "-" + day
            },
            getModelList: function () {
                let data = {
                    project_phases_id: this.datas.project_phases_id,
                    status: 2
                }

                this.$api.PROJECT_TEMPLATE_LIST(data).then((res) => {
                    if (res.code == 200) {
                        this.modelList = res.data.data
                    } else {
                        this.$message.error(res.msg)
                    }
                }).catch((err) => {

                })
            },
            templateChange: function (val) {

                let index = this.modelList.findIndex(a => a.project_template_name == val)
                if (index != -1) {
                    this.planChecked = false;
                    this.ganttChecked = false;

                    this.datas.project_template_name = this.modelList[index].project_template_name
                    this.datas.project_template_id = this.modelList[index].id

                    this.getModelDetails()
                }

            },
            getModelDetails: function () {
                this.$api.PROJECT_TEMPLATE_DETAIL(this.datas.project_template_id).then(res => {
                    if (res.code == 200) {
                        if (res.data){
                            this.datas.model = JSON.stringify(res.data);
                        }

                        this.plans = [];
                        this.isOutStart = [];
                        this.isOutEnd = [];
                        this.id = 1;
                        this.tableData = res.data.project_task
                        this.dealPlanData();
                    }
                }).catch(err => {

                })
            },
            refreshDate: function () {
                if (!this.tableData || !this.tableData.length) {
                    this.$message.error("请添加里程碑")
                    return
                }
                if (!this.datas.plan_start_time) {
                    this.$message.error("请设置项目计划开始时间")
                    return;
                }

                this.isOutStart = [];
                this.isOutEnd = [];
                this.clearDate(this.tableData)

                this.refreshTaskDate(this.tableData)

                if (this.noDateList.length) {
                    this.sss();
                }

                if (this.datas.plan_end_time) {
                    this.checkOutDate(this.tableData)
                }

                this.checkEndData(this.tableData);
            },
            clearDate: function (list) {
                for (let i = 0; i < list.length; i++) {
                    let data = list[i];
                    data.limitStart = ""
                    data.startDate = ""
                    data.start_date = ''
                    data.endDate = ''
                    data.end_date = ''
                    data.is_out = false;
                    data.outFront = false;

                    if (data.children && data.children.length > 0) {
                        this.clearDate(data.children);
                    }
                    this.$set(list, i, data)

                }
            },
            refreshTaskDate: function (list) {

                for (let i = 0; i < list.length; i++) {
                    let data = list[i];
                    let startDate = '';
                    if (data.level == 1) {
                        startDate = this.datas.plan_start_time
                    } else if (data.foreground_task_id && data.foreground_task_id != "") {
                        startDate = this.findData(data.foreground_task_id).endDate
                    } else {
                        startDate = this.findData(data.parent_id).startDate
                    }
                    if (startDate) {
                        data.limitStart = startDate;

                        data.startDate = this.dealDate(startDate, data.start_after_days ? data.start_after_days : 0)
                        data.start_date = data.startDate + " 00:00:00"

                        if (data.duration) {
                            data.endDate = this.dealEndDate(data.startDate, data.duration)
                            data.end_date = data.endDate + " 23:59:59"
                        }
                    } else {
                        this.noDateList.push(data)
                    }

                    if (data.children && data.children.length > 0) {
                        this.refreshTaskDate(data.children);
                    }

                }
            },
            dealPlanData: function () {

                if (!this.datas.plan_start_time) {
                    this.datas.plan_start_time = this.dealDateStr(new Date()) + " 00:00:00";

                    this.startDate = this.dealDateStr(new Date())
                }

                this.datas.plan_end_time = "";
                this.datas.duration = "";


                for (let i = 0; i < this.tableData.length; i++) {
                    let data = this.tableData[i];
                    data.limitStart = this.datas.plan_start_time;
                    data.name = data.task_name;

                    if (Number(data.id) >= this.id) {
                        this.id = Number(data.id) + 1;
                    }

                    data.startDate = this.dealDate(this.datas.plan_start_time, data.start_after_days)
                    data.start_date = data.startDate + " 00:00:00";
                    if (data.duration) {
                        data.endDate = this.dealEndDate(data.startDate, data.duration)
                        data.end_date = data.endDate + " 23:59:59"
                    }
                    this.plans.push(data);
                    if (data.files && data.files.length){
                        data.files.forEach(file=>{

                            if (file.id >= this.documents.id){

                                this.documents.id = Number(file.id) + 1
                            }

                        })
                    }
                    if (data.children && data.children.length > 0) {
                        this.dealChildren(data.children);
                    }
                }
                if (this.noDateList.length) {
                    this.sss();
                }

                this.checkEndData(this.tableData);
            },
            sss: function () {
                let arr = [...this.noDateList];
                this.noDateList = [];

                for (let i = 0; i < arr.length; i++) {
                    let data = arr[i];

                    let startDate = '';
                    if (data.foreground_task_id && data.foreground_task_id != "") {
                        startDate = this.findData(data.foreground_task_id).endDate
                    } else {
                        startDate = this.findData(data.parent_id).startDate
                    }
                    if (startDate) {
                        data.limitStart = startDate;

                        data.startDate = this.dealDate(startDate, data.start_after_days ? data.start_after_days : 0)
                        data.start_date = data.startDate + " 00:00:00"

                        if (data.duration) {
                            data.endDate = this.dealEndDate(data.startDate, data.duration)
                            data.end_date = data.endDate + " 23:59:59"
                        }
                    } else {
                        this.noDateList.push(data)
                    }
                }

                if (this.noDateList.length) {
                    this.sss();
                }
            },
            dealChildren: function (list) {

                for (let i = 0; i < list.length; i++) {
                    let data = list[i];
                    data.name = data.task_name;
                    let startDate = '';
                    if (data.foreground_task_id && data.foreground_task_id != "") {
                        startDate = this.findData(data.foreground_task_id).endDate
                    } else {
                        startDate = this.findData(data.parent_id).startDate
                    }
                    if (startDate) {
                        data.limitStart = startDate;

                        data.startDate = this.dealDate(startDate, data.start_after_days ? data.start_after_days : 0)
                        data.start_date = data.startDate + " 00:00:00"

                        if (data.duration) {
                            data.endDate = this.dealEndDate(data.startDate, data.duration)
                            data.end_date = data.endDate + " 23:59:59"
                        }
                    } else {
                        this.noDateList.push(data)
                    }

                    if (Number(data.id) >= this.id) {
                        this.id = Number(data.id) + 1;
                    }

                    this.plans.push(data);
                    if (data.children && data.children.length > 0) {
                        this.dealChildren(data.children);
                    }

                }
            },
            dealEndDate: function (str, days) {
                let date = new Date(str.replace(/-/g,'/'));

                if (Number(days)){
                    days = Number(days)-1
                }
                let timeStr = Number(days) * 24 * 3600;
                if (date.getTime() > 99999999999) {
                    timeStr = timeStr * 1000;
                }

                let dd = date.getTime() + timeStr

                let date2 = new Date(dd)

                let year = date2.getFullYear();
                let month = date2.getMonth() + 1;
                let day = date2.getDate();
                if (month < 10) {
                    month = "0" + month
                }
                if (day < 10) {
                    day = "0" + day;
                }

                return year + "-" + month + "-" + day
            },
            dealDate: function (str, days) {
                let date = new Date(str.replace(/-/g,'/'));

                let timeStr = Number(days) * 24 * 3600;
                if (date.getTime() > 99999999999) {
                    timeStr = timeStr * 1000;
                }

                let dd = date.getTime() + timeStr

                let date2 = new Date(dd)

                let year = date2.getFullYear();
                let month = date2.getMonth() + 1;
                let day = date2.getDate();
                if (month < 10) {
                    month = "0" + month
                }
                if (day < 10) {
                    day = "0" + day;
                }

                return year + "-" + month + "-" + day
            },
            /**
             * 时间发生更改
             * row: Object 当前行数据c
             */
            timeChange(row) {
                console.log("时间修改:", row);
            },
            //
            /**
             * 前置任务发生更改
             * row: Object 当前行数据
             * oldval: [String, Array] 前置修改前的旧数据
             * handle: Boolean 是否用户编辑产生的改变
             */
            preChange(row, oldval, handle) {
                console.log("前置修改:", row, oldval, handle);
            },
            // 数表展开行
            expandChange(row, expanded) {
                console.log("展开行:", row, expanded);
            },
            // 多选选择
            selectionChange(val) {
                console.log("多选：", val);
            },
            // 删除任务
            taskRemove(item) {
                console.log("删除任务：", item);
            },
            // 添加任务
            taskAdd(item) {
                console.log("添加任务：", item);
                // 非懒加载方式直接设置子数据
                /* this.$set(
                  item,
                  "children",
                  item.children.concat([
                    {
                      pid: item.id,
                      id: "###",
                      name: "一轮新月",
                      startDate: "2019-10-11",
                      endDate: "2019-10-19"
                    }
                  ])
                ); */
                this.$refs["wl-gantt-demo"].loadTreeAdd(item.id, [
                    {
                        pid: item.id,
                        id: "###",
                        name: "一轮新月",
                        startDate: "2019-10-11",
                        endDate: "2019-10-19",
                    },
                ]);
            },
            // 编辑任务
            editClick: function (item, index) {
                let data = JSON.parse(JSON.stringify(this.findData(item.id)))
                console.log(this.datas.member)
                if (item.parent_id) {
                    let parent = this.findData(item.parent_id)
                    this.members = parent.task_member ? parent.task_member : [];
                } else {
                    this.members = this.datas.member;
                }

                this.editItem = data;
                this.editId = data.id;
                this.isEdit = true;
                if (item.level == 1){
                    this.editTitle = "编辑里程碑"
                }else {
                    this.editTitle = "编辑任务"
                }

                this.editShow = true;
                setTimeout(() => {
                    if (this.$refs.editPlan) {
                        let arr = this.findDataArr(this.editId, this.tableData);
                        let list = [];
                        let data = [...this.tableData];
                        for (let i = 0; i < arr.length; i++) {
                            let a = data[arr[i]];
                            list.push(a.id);
                            if (arr.length > 1 && i != arr.length - 1) {
                                data = a.children;
                            }
                        }
                        this.$refs.editPlan.dealPlanList(list);
                    }
                }, 500)
            },
            // 添加里程碑
            addPlan: function () {
                let data = this.createData()

                this.members = this.datas.member;
                this.addId = -1;
                this.isEdit = false;
                this.editItem = data;
                this.editTitle = "新增里程碑"
                this.editShow = true;
                setTimeout(() => {
                    if (this.$refs.editPlan) {
                        this.$refs.editPlan.dealPlanList();
                    }
                }, 500)
            },
            // 添加子任务
            addChilen: function (item) {

                let num = item.level + 1

                let data = this.createData(item);

                this.members = item.task_member ? item.task_member : [];
                this.addId = item.id;
                this.isEdit = false;
                this.editItem = data;
                this.editTitle = "新增任务"
                this.editShow = true;
                setTimeout(() => {
                    if (this.$refs.editPlan) {
                        let arr = this.findDataArr(this.addId, this.tableData);
                        let list = [];
                        let data = [...this.tableData];
                        for (let i = 0; i < arr.length; i++) {
                            let a = data[arr[i]];
                            list.push(a.id);
                            if (arr.length > 1 && i != arr.length - 1) {
                                data = a.children;
                            }
                        }
                        this.$refs.editPlan.dealPlanList(list);
                    }
                }, 500)
            },
            // 创建数据生成
            dialogConfirm: function () {
                if (this.$refs.editPlan) {
                    this.$refs.editPlan.validateForm().then(val => {
                        let data1 = {...this.editItem}
                        data1.name = data1.task_name
                        data1.files.forEach(file => {
                            file.task_id = data1.id;
                            file.task_name = data1.task_name
                        })

                        if (this.datas.plan_start_time && data1.start_date) {
                            if (new Date(this.datas.plan_start_time.replace(/-/g,'/')).getTime() > new Date(data1.start_date.replace(/-/g,'/')).getTime()) {
                                this.isOutStart.push(data1.id);
                            } else {
                                let index = this.isOutStart.findIndex(a => a == data1.id)
                                if (index != -1) {
                                    this.isOutStart.splice(index, 1)
                                }
                            }
                        } else {
                            let index = this.isOutStart.findIndex(a => a == data1.id)
                            if (index != -1) {
                                this.isOutStart.splice(index, 1)
                            }
                        }
                        if (this.datas.plan_end_time && data1.end_date) {
                            if (new Date(this.datas.plan_end_time.replace(/-/g,'/')).getTime() < new Date(data1.end_date.replace(/-/g,'/')).getTime()) {
                                this.isOutEnd.push(data1.id);
                            } else {
                                let index = this.isOutEnd.findIndex(a => a == data1.id)
                                if (index != -1) {
                                    this.isOutEnd.splice(index, 1)
                                }
                            }
                        } else {
                            let index = this.isOutEnd.findIndex(a => a == data1.id)
                            if (index != -1) {
                                this.isOutEnd.splice(index, 1)
                            }
                        }
                        if (!this.isEdit) {
                            if (this.addId == -1) {
                                this.tableData.push(data1)
                            } else {
                                let ls = this.findDataArr(data1.parent_id, this.tableData)
                                let data = [...this.tableData]
                                for (let i = 0; i < ls.length; i++) {

                                    data = data[ls[i]].children
                                }
                                data.push(data1)
                            }
                            this.plans.push(data1);
                        } else {
                            let arr = this.findDataArr(this.editId, this.tableData)

                            let data2 = {}
                            if (arr.length > 1) {
                                let data = [...this.tableData]
                                for (let i = 0; i < arr.length; i ++){
                                    if (i == arr.length-1){
                                        data2 = {...data[arr[i]]};
                                        if (data1.parent_id == data2.parent_id){
                                            this.$set(data,arr[i],data1)
                                        }else {
                                            data.splice(arr[i],1)

                                            let parent = this.findData(data1.parent_id,this.tableData)
                                            if (!parent.children){
                                                parent.children = []
                                            }
                                            parent.children.push(data1)
                                        }

                                        break;
                                    }
                                    data = data[arr[i]].children
                                }
                            } else if (arr.length == 1) {
                                data2 = {...this.tableData[arr[0]]}
                                console.log(data1)
                                this.$set(this.tableData, arr[0], data1)

                            }

                            for (let i = 0; i < data2.files.length; i++) {
                                let doc = data2.files[i];
                                let index = this.documents.list.findIndex(a => a.id == doc.id);
                                if (index != -1) {
                                    this.documents.list.splice(index, 1)
                                }
                            }
                        }

                        this.documentDeal(data1)

                        this.checkEndData(this.tableData);

                        this.editShow = false;

                        if (this.planChecked){
                            this.planChange(this.planChecked)
                        }

                        if (this.keyDialog){
                            let index = this.keyData.findIndex(a=>a.id == data1.id)
                            if (index != -1){
                                let a = JSON.parse(JSON.stringify(data1))
                                a.children = [];
                                // this.keyData[index] = a;
                                this.$set(this.keyData,index,a)
                            }
                        }

                        this.$emit('save-change')
                    }).catch(err => {

                    })
                }

            },
            checkEndData: function (list) {
                for (let i = 0; i < list.length; i++) {
                    let data = list[i];
                    if (data.children && data.children.length > 0) {
                        this.checkEndData(data.children);
                    }
                    this.checkOut(data);
                    this.checkFront(data)
                }
            },
            checkOut: function (data) {
                data.is_out = false;
                if (data.endDate != "" && (data.children && data.children.length > 0)) {
                    let date = new Date(data.endDate.replace(/-/g,'/'));

                    for (let i = 0; i < data.children.length; i++) {
                        if (data.children[i].endDate != "") {
                            let date1 = new Date(data.children[i].endDate.replace(/-/g,'/'));
                            if (date1.getTime() > date.getTime() && date1.toLocaleDateString() != date.toLocaleDateString()) {
                                data.children[i].is_out = true;
                                data.is_out = true;
                            }
                        }
                    }
                }
            },
            checkFront:function(data){
                if (data.startDate && data.foreground_task_id){
                    let front = this.findData(data.foreground_task_id)
                    if (front.endDate){
                        let date = new Date(data.startDate.replace(/-/g,'/'));
                        let date1 = new Date(front.endDate.replace(/-/g,'/'))
                        if (date.getTime() < date1.getTime()){
                            data.outFront = true
                            return;
                        }
                    }
                }
                data.outFront = false
            },
            handleClose: function () {
                this.editShow = false;
            },
            // 删除任务
            deleteClick: function (id) {
                console.log(id)
                let arr = this.findDataArr(id, this.tableData)
                console.log(arr)
                if (arr.length > 1) {
                    let data = [...this.tableData]
                    for (let i = 0; i < arr.length; i++) {
                        if (i == arr.length - 2) {
                            data[arr[i]].children.splice(arr[arr.length - 1], 1)
                            break;
                        }
                        data = data[arr[i]].children
                    }
                } else if (arr.length == 1) {
                    this.tableData.splice(arr[0], 1)
                }

                let index = this.plans.findIndex(a => a.id == id)
                if (index != -1) {
                    let data = this.plans[index];
                    if (data.children && data.children.length) {
                        let list = this.getChildren(data.children);
                        list.push(id)
                        for (let i = 0; i < list.length; i++) {
                            let idx = this.plans.findIndex(a => a.id == list[i])
                            if (idx != -1) {
                                this.plans.splice(idx, 1);
                            }

                            let index1 = this.isOutStart.findIndex(a => a == list[i])
                            if (index1 != -1) {
                                this.isOutStart.splice(index1, 1)
                            }
                            let index2 = this.isOutEnd.findIndex(a => a == list[i])
                            if (index2 != -1) {
                                this.isOutEnd.splice(index2, 1)
                            }
                        }
                    } else {
                        this.plans.splice(index, 1);

                        let index1 = this.isOutStart.findIndex(a => a == id)
                        if (index1 != -1) {
                            this.isOutStart.splice(index1, 1)
                        }
                        let index2 = this.isOutEnd.findIndex(a => a == id)
                        if (index2 != -1) {
                            this.isOutEnd.splice(index2, 1)
                        }
                    }
                }

                if (this.keyDialog){
                    let index = this.keyData.findIndex(a=>a.id == id)
                    if (index != -1){
                        this.keyData.splice(index,1)

                    }
                }

                this.screenTableData()

                this.$emit('save-change')

            },
            getChildren: function (list) {
                let arr = [];
                for (let i = 0; i < list.length; i++) {
                    arr.push(list[i].id);
                    if (list[i].children && list[i].children.length) {
                        arr = arr.concat(this.getChildren(list[i].children))
                    }
                }
                return arr;
            },
            // 处理文档
            documentDeal: function (data) {

                for (let i = 0; i < data.files.length; i++) {

                    let doc = data.files[i];
                    doc.task_id = data.id;
                    doc.task_name = data.task_name;

                    let index = this.documents.list.findIndex(a => a.id == doc.id);

                    if (index == -1) {
                        this.documents.list.push(doc)
                    } else {
                        this.$set(this.documents.list, index, doc);
                    }

                }

            },
            // 检查上移
            checkTop: function (id) {
                let arr = this.findDataArr(id, this.tableData)
                if (arr.length > 0 && arr[arr.length - 1]) {
                    return true
                } else {
                    return false
                }
            },
            // 检查下移
            checkBottom: function (id) {
                let arr = this.findDataArr(id, this.tableData)

                let length = 0;
                if (arr.length > 1) {
                    let data = [...this.tableData]
                    for (let i = 0; i < arr.length; i++) {
                        data = data[arr[i]].children
                        if (i == arr.length - 2) {
                            length = data.length;
                            break;
                        }
                    }
                } else if (arr.length == 1) {
                    length = this.tableData.length;
                }

                if (arr.length > 0 && arr[arr.length - 1] != length - 1) {
                    return true
                } else {
                    return false
                }
            },
            bottomClick: function (id) {
                let arr = this.findDataArr(id, this.tableData)

                if (arr.length > 1) {
                    let data = [...this.tableData]
                    for (let i = 0; i < arr.length; i++) {
                        if (i == arr.length - 2) {
                            this.downGo(data[arr[i]].children, arr[i + 1])
                            break;
                        }
                        data = data[arr[i]].children
                    }
                } else if (arr.length == 1) {
                    this.downGo(this.tableData, arr[0])
                }
            },
            topClick: function (id) {
                let arr = this.findDataArr(id, this.tableData)


                if (arr.length > 1) {
                    let data = [...this.tableData]
                    for (let i = 0; i < arr.length; i++) {
                        if (i == arr.length - 2) {
                            this.upGo(data[arr[i]].children, arr[i + 1])
                            break;
                        }
                        data = data[arr[i]].children
                    }
                } else if (arr.length == 1) {
                    this.upGo(this.tableData, arr[0])
                }
            },
            downGo: function (arr, index) {
                arr[index] = arr.splice(index + 1, 1, arr[index])[0];
            },
            upGo: function (arr, index) {
                arr[index] = arr.splice(index - 1, 1, arr[index])[0];
            },
            // 查找任务数据
            findData: function (id) {
                let arr = this.findDataArr(id, this.tableData);

                let data = {}
                if (arr.length == 1) {
                    data = {...this.tableData[arr[0]]}
                } else {
                    let a = [...this.tableData]
                    for (let i = 0; i < arr.length; i++) {
                        if (i == arr.length - 1) {
                            data = {...a[arr[i]]};
                        } else {
                            a = a[arr[i]].children;
                        }
                    }
                }
                return data;
            },
            // 查找任务路径
            findDataArr: function (id, list) {
                let arr = [];
                for (let i = 0; i < list.length; i++) {
                    if (list[i].id == id) {
                        arr.push(i)
                        break
                    } else if (list[i].children && list[i].children.length > 0) {
                        let a = this.findDataArr(id, list[i].children)
                        if (a.length > 0) {
                            arr.push(i);
                            arr = arr.concat(a);
                            break;
                        }
                    }
                }

                return arr;
            },
            planChange: function (value) {
                if (value) {
                    this.screenTableData();
                }
            },
            ganttChange: function (value) {

            },
            keyChange:function(value){

            },
            onKey:function(){

                this.$api.TASK_CAL_LINE({data:this.tableData}).then(res=>{
                    if (res.code == 200){
                        this.keyDialog = true;
                        if (res.data){
                            res.data.forEach(item=>{
                                item.children = []
                                item.startDate = item.start_date
                                item.endDate = item.end_date
                                item.name = item.task_name
                            })
                            this.keyData = res.data
                        }

                    }else{
                        this.$message.error(res.msg)
                    }

                })
            },
            createData: function (item = null) {
                let data = {};
                data.id = this.id + "";
                data.task_name = ''
                data.level = item ? item.level + 1 : 1
                data.startDate = ""
                data.realStartDate = ""
                data.endDate = ""
                data.realEndDate = ""
                data.parent_id = item ? item.id : ""
                data.parent_name = item ? item.task_name : ''
                data.foreground_task_id = "";
                data.foreground_task_name = ''
                data.start_after_days = 0
                data.duration = 0
                data.periodical_review = false;
                data.mandatory_check = false;
                data.files = [];
                data.children = [];
                data.task_member = []
                data.task_member_id = []
                data.is_add = true;
                data.person_in_charge = {}
                data.person_id = "";
                data.person = "";

                this.id++;
                return data;
            },
            deleteDoc: function (doc) {
                let arr = this.findDataArr(doc.task_id, this.tableData)

                let plan = {}
                if (arr.length > 1) {
                    let data = [...this.tableData]
                    for (let i = 0; i < arr.length; i++) {
                        if (i == arr.length - 1) {
                            plan = data[arr[i]]
                            break;
                        }
                        data = data[arr[i]].children
                    }
                } else if (arr.length == 1) {
                    plan = this.tableData[arr[0]]
                }

                let index = plan.files.findIndex(a => a.id = doc.id)
                if (index != -1) {
                    plan.files.splice(index, 1);
                }
                this.$emit('save-change')
            },
            addDoc: function (doc) {
                let arr = this.findDataArr(doc.task_id, this.tableData)

                let plan = {}
                if (arr.length > 1) {
                    let data = [...this.tableData]
                    for (let i = 0; i < arr.length; i++) {
                        if (i == arr.length - 1) {
                            plan = data[arr[i]]
                            break;
                        }
                        data = data[arr[i]].children
                    }
                } else if (arr.length == 1) {
                    plan = this.tableData[arr[0]]
                }
                plan.files.push(doc)
                this.$emit('save-change')
            }
        }
    }
</script>

<style lang="scss">
  @import "../../../../../assets/scss/views/projectManagement/list/planView.scss";
</style>
